package com.shiguiwu.springmybatis.spring.aop.demo;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;

import java.lang.reflect.Method;
import java.util.Objects;

/**
 * @description: Pointcut 切入点
 *
 * @author: stone
 * @date: Created by 2021/5/19 14:54
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.aop.demo
 */
public class PointcutV2 implements Pointcut {

    @Override
    public ClassFilter getClassFilter() {
        //是否是目标类型
        return UserService.class::isAssignableFrom;
    }

    @Override
    public MethodMatcher getMethodMatcher() {
        return new MethodMatcher() {
            @Override
            public boolean matches(Method method, Class<?> aClass) {
                //判断是否为work方法
                return "work".equalsIgnoreCase(method.getName());
            }

            @Override
            public boolean isRuntime() {
                return true;
            }

            @Override
            public boolean matches(Method method, Class<?> aClass, Object... objects) {
                //动态匹配@2：isRuntime为true的时候，会执行这个方法
                if (objects.length == 1) {
                    String username = (String) objects[0];
                    return username.contains("粉丝");
                }
                return false;
            }
        };
    }


}
